package com.lagou.service.impl;

import com.lagou.dao.RolesMapper;
import com.lagou.domain.*;
import com.lagou.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RolesServiceImpl implements RolesService {

    @Autowired
    private RolesMapper rolesMapper;

    @Override
    public List<Role> findAllRole(Role role) {
        return rolesMapper.findAllRole(role);
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {
        return rolesMapper.findMenuByRoleId(roleId);
    }

    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {

        rolesMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        Date date = new Date();
        for (Integer mid : roleMenuVo.getMenuIdList()) {

            Role_menu_relation relation = new Role_menu_relation();
            relation.setRoleId(roleMenuVo.getRoleId());
            relation.setMenuId(mid);
            relation.setCreatedBy("system");
            relation.setUpdatedby("system");
            relation.setCreatedTime(date);
            relation.setUpdatedTime(date);

            rolesMapper.roleContextMenu(relation);
        }
    }

    @Override
    public void deleteRole(Integer roleId) {

        rolesMapper.deleteRoleContextMenu(roleId);

        rolesMapper.deleteRole(roleId);
    }

    /**
     * 给角色分配资源
     * @param roleResourceVo
     */
    @Override
    public void roleContextResource(RoleResourceVo roleResourceVo) {

        //1. 删除角色的旧的资源关系
        rolesMapper.deleteRoleContextResource(roleResourceVo.getRoleId());

        //2. 给角色分配新的资源关系
        Date date = new Date();
        for (Integer resourceId : roleResourceVo.getResourceIdList()) {

            RoleResourceRelation relation = new RoleResourceRelation();
            relation.setResourceId(resourceId);
            relation.setRoleId(roleResourceVo.getRoleId());
            relation.setCreatedTime(date);
            relation.setUpdatedTime(date);
            relation.setCreatedBy("system");
            relation.setUpdatedBy("system");
            rolesMapper.roleContextResource(relation);
        }
    }

    /**
     * 获取当前角色拥有的资源信息
     * @param roleId
     * @return
     */
    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer roleId) {

        List<ResourceCategory> resourceCategory = rolesMapper.findResourceCategoryByRoleId(roleId);

        List<Resource> resourceList = rolesMapper.findResourceByRoleId(roleId);

        for (Resource resource : resourceList) {
            for (ResourceCategory category : resourceCategory) {
                if (resource.getCategoryId() == category.getId()) {
                    category.addResource(resource);
                    break;
                }
            }
        }

        return resourceCategory;
    }
}
